package org.dioib.practice.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dioib.practice.mapper.DiscardSysUserMapper;
import org.dioib.practice.mapper.SysUserMapper;
import org.dioib.practice.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysUserService{

    @Autowired
    private DiscardSysUserMapper discardSysUserMapper;

    public int save(SysUser sysUser){
        if(sysUser.getId() != null && sysUser.getId() != 0) {
            System.out.println("【“+this.getClass().getEnclosingMethod().getName()+”】");
            return discardSysUserMapper.updateSysUser(sysUser);
        }else{
            System.out.println("【“+this.getClass().getEnclosingMethod().getName()+”】");
            return discardSysUserMapper.insert(sysUser);
        }
    }

    public Map<String, Object> findAll(Integer pageNum, Integer pageSize, SysUser sysUser) {
        System.out.println("【“+this.getClass().getEnclosingMethod().getName()+”】");
        Map<String, Object> findMap = new HashMap<>();
        findMap.put("sysUser", sysUser);

        Integer total = discardSysUserMapper.totalSelect(findMap);
        findMap.put("pageNum",  pageNum > 0 ? (pageNum-1)*pageSize : 1);
        findMap.put("pageSize", pageSize);
        findMap.put("total", total);

        List<SysUser> userlist = discardSysUserMapper.findByPage(findMap);
        findMap.remove("sysUser");
        findMap.put("data", userlist);

        return findMap;
    }
}
